package com.syb.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.syb.dao.ResourcesDao;
import com.syb.entity.function.SysResource;
import com.syb.util.BaseDao;
import com.syb.util.PageResults;

@Repository
public class ResourcesDaoImpl extends BaseDao implements ResourcesDao {

	
	@Override
	public List<SysResource> findResourcesList(PageResults<SysResource> page) throws Exception {
		StringBuffer sub = new StringBuffer();
		List<Object> paramList = new ArrayList<Object>(); 
		List<SysResource> result = new ArrayList<SysResource>();
		sub.append(" from SysResource where 1=1");
		if(page.getParamMap().get("name")!=null && !("").equals(page.getParamMap().get("name"))){
			sub.append(" and resource_name like ? ");
			paramList.add("%"+page.getParamMap().get("name")+"%");
		}
		/*sub.append(" limit ?,? ");*/
		/*paramList.add(page.getCurrentPage());
		paramList.add(page.getPageSize());*/
		try{
		Query query = this.getSessionFactory().getCurrentSession().createQuery(sub.toString());
		query.setFirstResult(page.getCurrentPage());
		query.setMaxResults(page.getPageSize());
		if(paramList.size()>0){
			for(int i=0;i<paramList.size();i++){
				query.setParameter(i, paramList.get(i));
			}
		}
		result = query.list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return result;
	}

	@Override
	public int findResourcesCount(PageResults<SysResource> page) throws Exception {
		StringBuffer sub = new StringBuffer();
		List<Object> paramList = new ArrayList<Object>(); 
		List<BigInteger> result = new ArrayList<BigInteger>();
		sub.append("select count(*)  from sys_resource where 1=1");
		if(page.getParamMap().get("name")!=null && !("").equals(page.getParamMap().get("name"))){
			sub.append(" and resource_name like ? ");
			paramList.add("%"+page.getParamMap().get("name")+"%");
		}
		try{
		Query query = this.getSessionFactory().getCurrentSession().createSQLQuery(sub.toString());
		if(paramList.size()>0){
			for(int i=0;i<paramList.size();i++){
				query.setParameter(i, paramList.get(i));
			}
		}
		result = query.list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return result.get(0).intValue();
	}

	@Override
	public List<SysResource> findResorcesDing() throws Exception {
		List<SysResource> resourcesList = new ArrayList<SysResource>();
		String sql = "select * from sys_resource where text = '0'";
		try{
		 resourcesList = this.getSessionFactory().getCurrentSession().createSQLQuery(sql).addEntity(SysResource.class).list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
		return resourcesList;
	}

	@Override
	public void insertResource(SysResource sys) throws Exception {
		List<String> list = new ArrayList<String>();
		String sql;
		if(sys.getText()!=null){
			list.add(sys.getResourceName());
			list.add(sys.getResourceUrl());
			list.add(sys.getText());
		 sql = "insert into sys_resource (resource_name,resource_url,text) value (?,?,?)";
		}else{
			list.add(sys.getResourceName());
			list.add(sys.getResourceUrl());
			list.add(sys.getParentId().toString());
		 sql = "insert into sys_resource (resource_name,resource_url,parent_id) value (?,?,?)";
		}
		 try{
			 Query query = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			 for(int i=0;i<list.size();i++){
				 query.setParameter(i, list.get(i));
			 }
			 query.executeUpdate();
			  }catch(Exception e){
				  throw new Exception(e.getMessage());
		  }
	}

	@Override
	public void delResource(SysResource sys) throws Exception {
		try{
			  this.getSessionFactory().getCurrentSession().delete(sys);
			  }catch(Exception e){
				  throw new Exception(e.getMessage());
		  }
		
	}

	@Override
	public void updateResource(SysResource sys) throws Exception {
		List<String> paramList = new ArrayList<String>();
		StringBuffer sub = new StringBuffer();
		sub.append("update sys_resource set ");
		if(sys.getResourceName()!=null&&!sys.getResourceName().equals("")){
			sub.append("  resource_name=? ");
			paramList.add(sys.getResourceName());
		}
		if(sys.getResourceUrl()!=null&&!sys.getResourceUrl().equals("")){
			sub.append(" , resource_url=? ");
			paramList.add(sys.getResourceUrl());
		}
		if(sys.getParentId()!=null&&!sys.getParentId().equals("")){
			sub.append(" , parent_id=? ");
			paramList.add(sys.getParentId().toString());
		}
		if(sys.getText()!=null&&!sys.getText().equals("")){
			sub.append(" , text=? ");
			paramList.add(sys.getText());
		}
		if(sys.getId()!=null&& !sys.getId().equals("")){
			sub.append(" where  id=? ");
			paramList.add(sys.getId().toString());
		}
		String sql = sub.toString();
		try{
			Query query = this.getSessionFactory().getCurrentSession().createSQLQuery(sql).addEntity(SysResource.class);
			if(paramList.size()>0){
				for(int i=0;i<paramList.size();i++){
					query.setParameter(i, paramList.get(i));
				}
			}
			query.executeUpdate();
			  }catch(Exception e){
				  throw new Exception(e.getMessage());
		  }
	}

	@Override
	public List<SysResource> findResourceByName(SysResource name) throws Exception {
		List<SysResource> sys = new ArrayList<SysResource>();
		List<String> paramList = new ArrayList<String>();
		StringBuffer sub = new StringBuffer();
		sub.append("select * from sys_resource where 1=1 ");
		if(name.getResourceName()!=null && !name.getResourceName().equals("")){
			sub.append(" and resource_name = ?");
			paramList.add(name.getResourceName());
		}
		if(name.getId()!=null && !name.getId().equals("")){
			sub.append(" and id = ?");
			paramList.add(name.getId().toString());
		}
		
		String sql = sub.toString();
		try{
			Query query = this.getSessionFactory().getCurrentSession().createSQLQuery(sql).addEntity(SysResource.class);
			if(paramList.size()>0){
				for(int i=0;i<paramList.size();i++){
					query.setParameter(i, paramList.get(i));
				}
			}
			sys = query.list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return sys;
	}

	@Override
	public List<SysResource> findResourceList() throws Exception {
		List<SysResource> sysList = new ArrayList<SysResource>();
		try{
			sysList = this.getSessionFactory().getCurrentSession().createQuery(" from SysResource").list();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		
		return sysList;
	}

}
